package com.alog.storage.utils;

import android.os.AsyncTask;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程池工具类
 */
public class ThreadPoolUtil {
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
    private static final int KEEP_ALIVE = 60;
    private static final BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>(128);
    private static ThreadPoolExecutor threadPool;

    private ThreadPoolUtil() {
    }

    static {
        AsyncTask a = null;
        threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, workQueue, new ThreadFactory() {
            private final AtomicInteger integer = new AtomicInteger();

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "thread:" + integer.getAndIncrement());
            }
        });
    }

    public static void execute(Runnable runnable) {
        threadPool.execute(runnable);
    }

}